<?php

namespace app\api\model;

use app\common\model\TeacherCourse as TeacherCourseModel;

class TeacherCourse extends TeacherCourseModel
{

    /**
     * 获取推荐课程
     */
    public static function getByRecommend($languageId = 0, $courseId = 0)
    {
        $where = static::whereOnline();
        $where['recommendswitch'] = ['>', 0];
        if ($languageId) {
            $where['language_id'] = ['=', $languageId];
            $where['teacher_course_id'] = ['<>', $courseId];
        }
        return self::with(['teacher' => function ($query) {
            $query->with(['tag']);
        }, 'language', 'grade'])->withCount('course')->where($where)->order(static::WEIGH_DESC)->select();
    }

    /**
     * 课程详情
     */
    public static function getByTeacherCourseId($courseId, $more = false)
    {
        $course = self::with(['language', 'grade', 'teacher' => function ($query) use ($more) {
            if ($more) {
                $query->with(['tag', 'experience', 'images']);
            }
        } ])->withCount('course')->find($courseId);
        if (!$course) {
            static::error(10008);
        }
        if (!$course->teacher) {
            static::error(405);
        }
        if (!$course->onlineswitch) {
            static::error(10001);
        }
        if ($more) {
            $course['comments'] = CourseComment::getCommentsByLimit($courseId);
            $course['recommend'] = self::getByRecommend($course->language_id, $courseId);
            $course['comment_count'] = CourseComment::getCommentCounts($course->teacher_course_id);
        }

        return $course;
    }

    /**
     * 更新
     */
    public static function updateData($teacherCourseId, $data)
    {
        return self::where('teacher_course_id', $teacherCourseId)->update($data);
    }

    /**
     * 收藏课程列表
     * @param $courseCollect 收藏的课程ID
     * @param $courseOrder 曾授课的课程ID
     * @param bool $ever true:获取曾授课的，false：获取未授课的
     */
    public static function getCollectTeacherCourses($userId, $size = 20, $ever = false)
    {
        //收藏的课程ID
        $courseCollect = TeacherCourseCollect::getByUserId($userId);
        //曾授课的课程ID
        $courseOrder = array_values(array_unique(CourseOrder::getColumnByUserId($userId, [2, 3], 'teacher_course_id')));
        //授课又收藏的
        $collectCourseId = array_intersect($courseCollect, $courseOrder);
        //我的收藏（未授过课的）
        $teacherCourse = self::collect($courseCollect, $collectCourseId, $size);
        if ($ever) {
            $teacherCourse =  self::ever($collectCourseId, $size);
        }
        return $teacherCourse;
    }

    /**
     * 曾授课
     */
    public static function study($teacherCourseId, $size = 20)
    {
        return self::with(['language', 'grade', 'teacher' => function ($query) {
            $query->with(['tag']);
        }])
            ->withCount('course')
            ->whereIn('teacher_course_id', $teacherCourseId)
            ->where(static::whereOnline())
            ->order(static::WEIGH_DESC)
            ->paginate($size);
    }

    /**
     * 我的收藏
     */
    public static function collect($userId, $size = 20)
    {
        //收藏的课程ID
        $teacherCourseId = TeacherCourseCollect::getByUserId($userId);
        return self::study($teacherCourseId, $size);
    }

    /**
     * 曾授课
     */
    public static function ever($userId, $size)
    {
        //曾授课的课程ID
        $teacherCourseId = array_values(array_unique(CourseOrder::getColumnByUserId($userId, [2, 3], 'teacher_course_id')));
        return self::study($teacherCourseId, $size);
    }



    /**
     * 获取语种
     */
    public static function getLanguageCounts($userId)
    {
        $teachreCourseId = Course::getColumnByUserId($userId, 4, 'teacher_course_id');
        $languageCounts = self::whereIn('teacher_course_id', $teachreCourseId)->column('language_id');
        return count(array_unique($languageCounts));
    }

    public static function getBySearch($name)
    {
        $teacherId = Teacher::getBySearch($name);
        $teacherCourseId =  self::whereIn('teacher_id', $teacherId)->column('teacher_course_id');
        return $teacherCourseId ?: [];
    }

    public static function getGradeId($name)
    {
        $gradeId = self::whereIn('teacher_course_id', self::getBySearch($name))->column('language_grade_id');
        return $gradeId ?: [];
    }
}
